package club.banyuan.demo.demo7;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @author edz
 * @version 1.0
 * @date 2020/12/22 下午5:10
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> c = Class.forName("club.banyuan.demo.demo7.Student");

//        Field[] fields = c.getFields();

//        Field[] df = c.getDeclaredFields();
//        for (Field field : df) {
//            System.out.println(field);
//        }

        Field name = c.getField("name");

        //获取无参构造方法创建对象
        Constructor<?> con = c.getConstructor();
        Object obj = con.newInstance();

        //给obj的成员变量name赋值为西安
        name.set(obj,"西安");

    }
}
